<?php

namespace app\admin\controller;

use think\Session;
use app\admin\model\AdminUsers;
use tools\AdminAuth;
use tools\GeetestLib;

class Login extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if ($this->request->isPost()) {
            /*if (!$this->check_geetest()) {
                return $this->error('验证失败');
            }*/
            $result = validate('AdminUser');
            if (true !== $result->scene('login')->check($this->param)) {
                return $this->error($result->getError());
            }
            $data = [
                'name' => $this->param['name'],
                'password' => md5($this->param['password']),
            ];
            $user = AdminUsers::get($data);
            if (!$user) {
                return $this->error('用户名或密码错误');
            }
            if ($user->status != 1) {
                return $this->error('账户被冻结');
            }
            $remember = isset($this->param['remember']) ? true : false;
            AdminAuth::login($user['id'], $user['name'], $remember);

            //手动加入日志
            $auth = new AdminAuth();
            $this->request->param('password', '');
            $auth->createLog('登录', 2);
            return $this->success('登录成功','/admin');
        }

        return view();
    }

    //使用前验证
    public function get_geetest_status()
    {
        $geetest = new GeetestLib(config('geetest.id'), config('geetest.key'));
        $data = array(
            "user_id" => "0", # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => $this->request->ip() # 请在此处传输用户请求验证时所携带的IP
        );

        $status = $geetest->pre_process($data, 1);
        Session::set('gtserver', $status);
        Session::set('gt_user_id', $data['user_id']);

        return json($geetest->get_response_str());
    }

    protected function check_geetest()
    {
        $geetest = new GeetestLib(config('geetest.id'), config('geetest.key'));
        $data = array(
            "user_id" => Session::get('gt_user_id'), # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => $this->request->ip()
        );

        if (Session::get('gtserver') == 1) {   //服务器正常
            $result = $geetest->success_validate($this->param['geetest_challenge'], $this->param['geetest_validate'], $this->param['geetest_seccode'], $data);
            if ($result) {
                return true;
            }
        } else {  //服务器宕机,走failback模式
            if ($geetest->fail_validate($this->param['geetest_challenge'], $this->param['geetest_validate'])) {
                return true;
            }
        }
        return false;
    }

    //退出
    public function logout()
    {
        $auth = new AdminAuth();
        $auth->createLog('退出', 2);
        $auth->logout();
        $this->redirect('index');
    }

}
